package com.deyuanyun.pic.pipe.controller;

import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.pipe.controller.dto.ProtectionQuery;
import com.deyuanyun.pic.pipe.controller.vo.ProtectionVO;
import com.deyuanyun.pic.pipe.enumeration.PipeExportEnum;
import com.deyuanyun.pic.pipe.service.PipeMgrService;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “水工保护”控制类
 *
 * @author xiatao
 * 时间:2015年08月20日 14时26分39秒
 * 水工保护controller层
 */
@Controller
@RequestMapping("/pipe/protection")
public class ProtectionController extends PipeBaseController {

    @Resource
    private PipeMgrService pipeMgrService;

    /**
     * 跳转到 水工保护 主页面
     *
     * @return
     */
    @RequestMapping("/toProtection.html")
    public ModelAndView toProtection(){
        ModelAndView mv=new ModelAndView();
        mv.setViewName("basedata/pipe/protection");
        return mv;
    }

    /**
     * 根据条件分页查询 水工保护 数组对象
     *
     * @param protectionQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(ProtectionQuery protectionQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(this.queryVO(protectionQuery));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 根据条件导出 水工保护
     *
     * @param protectionQuery
     * @throws IOException
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(ProtectionQuery protectionQuery) throws IOException {
        List list = this.queryVO(protectionQuery);
        PipeExportEnum exportEnum = PipeExportEnum.Protection;
        String[] columnKeys = {"pipeLineName", "protectionName", "protectionNumber", "fromMileage", "fromReference",
                "fromReferenceDistance", "toMileage", "toReference", "toReferenceDistance", "locationDescription",
                "materialType", "protectionType", "protectionSize", "state", "inserviceDate",
                "assemblyCompany", "inspectingCompany", "testingcompany", "recorder", "recordTime", "remark"};
        DownExcelUtil.export(exportEnum.getFileName(), pipePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
    }

    private List queryVO(ProtectionQuery protectionQuery) {
        return new ProtectionVO().fromDomainList(pipeMgrService.queryProtection(protectionQuery));
    }
}
